package com.dandelion.admin.manager;

import com.dandelion.admin.domain.OrderListItem;
import com.dandelion.common.domain.ExprStatusEnum;
import com.dandelion.common.domain.OrderStatusEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class OrderExportManager {

    public HSSFWorkbook export(List<OrderListItem> list) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("data");
        int rowIndex = 0;
        newAndInitHeader(sheet, rowIndex);

        if (CollectionUtils.isEmpty(list)) {
            return workbook;
        }

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (OrderListItem item : list) {
            newAndBuildRow(sheet, ++rowIndex, item, rowIndex, format);
        }

        return workbook;
    }

    private void newAndBuildRow(HSSFSheet sheet, int rowIndex, OrderListItem item, int index, DateFormat format) {
        int colIndex = 0;
        HSSFRow row = sheet.createRow(rowIndex);

        row.createCell(colIndex++).setCellValue(index);
        row.createCell(colIndex++).setCellValue(item.getOrderNo());
        row.createCell(colIndex++).setCellValue(item.getOrderCctName());
        row.createCell(colIndex++).setCellValue(item.getOrderCctMobile());
        row.createCell(colIndex++).setCellValue(StringUtils.defaultString(item.getTopCatName()));

        row.createCell(colIndex++).setCellValue(StringUtils.defaultString(item.getSvrName()));
        row.createCell(colIndex++).setCellValue(item.getCstName());
        row.createCell(colIndex++).setCellValue(item.getCstMobile());
        row.createCell(colIndex++).setCellValue(toFullAddress(item.getCstAddrProv(), item.getCstAddrCity(), item.getCstAddrArea(), item.getCstAddrDist(), item.getCstAddrDetail()));
        row.createCell(colIndex++).setCellValue(item.getExprOrderNo());

        row.createCell(colIndex++).setCellValue(item.getExprComName());
        row.createCell(colIndex++).setCellValue(item.getPkAddr());
        row.createCell(colIndex++).setCellValue(item.getPkCctName());
        row.createCell(colIndex++).setCellValue(item.getPkCctMobile());
        row.createCell(colIndex++).setCellValue(ExprStatusEnum.of(NumberUtils.toByte(item.getExprStatus())).getStatusName());

        row.createCell(colIndex++).setCellValue(item.getCreateTime() == null ? "" : format.format(item.getCreateTime()));
    }

    private void newAndInitHeader(HSSFSheet sheet, int rowIndex) {
        HSSFRow header = sheet.createRow(rowIndex);
        int colIndex = 0;
        header.createCell(colIndex++).setCellValue("序号");
        header.createCell(colIndex++).setCellValue("订单号");
        header.createCell(colIndex++).setCellValue("订单联系人");
        header.createCell(colIndex++).setCellValue("订单联系人手机");
        header.createCell(colIndex++).setCellValue("服务类目");

        header.createCell(colIndex++).setCellValue("服务类型");
        header.createCell(colIndex++).setCellValue("客户姓名");
        header.createCell(colIndex++).setCellValue("客户手机");
        header.createCell(colIndex++).setCellValue("客户地址");
        header.createCell(colIndex++).setCellValue("货运单号");

        header.createCell(colIndex++).setCellValue("物流公司");
        header.createCell(colIndex++).setCellValue("提货地址");
        header.createCell(colIndex++).setCellValue("提货联系人");
        header.createCell(colIndex++).setCellValue("提货联系人手机");
        header.createCell(colIndex++).setCellValue("货物状态");

        header.createCell(colIndex++).setCellValue("创建时间");
    }

    private static String toFullAddress(String province, String city, String area, String dist, String detail) {
        StringBuilder builder = new StringBuilder(64);
        if (StringUtils.isNotBlank(province)) {
            builder.append(province);
        }
        if (StringUtils.isNotBlank(city)) {
            builder.append(city);
        }
        if (StringUtils.isNotBlank(area)) {
            builder.append(area);
        }
        if (StringUtils.isNotBlank(dist)) {
            builder.append(dist);
        }
        if (StringUtils.isNotBlank(detail)) {
            builder.append(detail);
        }
        return builder.toString();
    }
}
